package com.hiray.demos.ui_architect.litho

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.OrientationHelper
import com.facebook.litho.ComponentContext
import com.facebook.litho.LithoView
import com.facebook.litho.widget.LinearLayoutInfo
import com.facebook.litho.widget.Recycler
import com.facebook.litho.widget.RecyclerBinder
import com.hiray.demos.R
import com.hiray.demos.ui_architect.litho.list.BusinessInfo
import com.hiray.demos.ui_architect.litho.list.EleBusinessItem
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration

class EleBusinessListActivity : AppCompatActivity() {

    val imageUrls = arrayListOf(
            "https://fuss10.elemecdn.com/1/87/91067ea1fd91c853b55912b939cf1png.png?imageMogr/format/webp/thumbnail/360x/",
            "https://fuss10.elemecdn.com/4/88/d7c7bf770a6a10668da61b397db5bpng.png?imageMogr/format/webp/thumbnail/360x/",
            "https://fuss10.elemecdn.com/3/f7/3a3dcea450c2851a80f32286c614bpng.png?imageMogr/format/webp/thumbnail/360x/",
            "https://fuss10.elemecdn.com/6/e1/5a26794ff98c7f1884a728bcd8cc1png.png?imageMogr/format/webp/thumbnail/360x/",
            "https://fuss10.elemecdn.com/d/1a/c6d067e659c290e3b303937759e1dpng.png?imageMogr/format/webp/thumbnail/360x/",
            "https://fuss10.elemecdn.com/f/ac/c85ab567c1331f8e8cd4ebb08572ajpeg.jpeg?imageMogr/format/webp/thumbnail/360x/",
            "https://fuss10.elemecdn.com/7/74/567ea56e564ab4a764e15f5c0c0f8jpeg.jpeg?imageMogr/format/webp/thumbnail/360x/",
            "https://fuss10.elemecdn.com/8/db/76957d6ec0e388751f6bf2b01e880png.png?imageMogr/format/webp/thumbnail/360x/",
            "https://fuss10.elemecdn.com/3/67/52286d5672ff59e74c8275fa23a0cpng.png?imageMogr/format/webp/thumbnail/360x/",
            "https://fuss10.elemecdn.com/7/d0/69a27647d7ce66c5c43d18af8de30jpeg.jpeg?imageMogr/format/webp/thumbnail/360x/",
            "https://fuss10.elemecdn.com/0/38/9654a9eeb37fced07ed91ab3b93adpng.png?imageMogr/format/webp/thumbnail/360x/",
            "https://fuss10.elemecdn.com/b/b5/c2a709f41e85b0c882bc33676ddc7png.png?imageMogr/format/webp/thumbnail/360x/",
            "https://fuss10.elemecdn.com/c/45/05acfbe4e0e2c10b77e183f16b69ejpeg.jpeg?imageMogr/format/webp/thumbnail/360x/",
            "https://fuss10.elemecdn.com/c/cf/2f1c9ea0a184dd984a43854d84b6bjpeg.jpeg?imageMogr/format/webp/thumbnail/360x/",
            "https://fuss10.elemecdn.com/3/7e/3446db702f44c4d863b742ac034b2png.png?imageMogr/format/webp/thumbnail/360x/",
            "https://fuss10.elemecdn.com/1/4d/13ed8f102f0b8c34036796f856285png.png?imageMogr/format/webp/thumbnail/360x/")

    val businessNames = arrayListOf(
            "好多肉汉堡（西溪泊岸店）",
            "云荷香居叶饭（仓前店）",
            "咕咕韩式炸鸡",
            "咕么咕么蒸鸡米粉",
            "汉霸汉堡",
            "东北饺子馆",
            "老家私房菜",
            "姜员外",
            "青蔬捞烫",
            "蹭饭",
            "如意混沌（赛银国际店）",
            "低卡实验室（西溪店）",
            "和府捞面",
            "老底子杭帮味道",
            "五芳斋",
            "猛男炒饭"
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val context = ComponentContext(this)

        val binder = RecyclerBinder.Builder()
                .rangeRatio(4f)
                .layoutInfo(LinearLayoutInfo(context, OrientationHelper.HORIZONTAL, false))
                .build(context)
        addToBinder(context, binder)
        val recyclerView = Recycler.create(context)
                .itemDecoration(HorizontalDividerItemDecoration.Builder(this)
                        .colorResId(R.color.divider_light)
                        .margin(dp2px(95f).toInt(), 0)
                        .size(2)
                        .build())
                .binder(binder)
                .build()

        setContentView(LithoView.create(context, recyclerView))
    }

    private fun addToBinder(c: ComponentContext, binder: RecyclerBinder) {
        imageUrls.forEachIndexed { index, imageUrl ->

            binder.insertItemAt(
                    index,
                    EleBusinessItem.create(c).businessInfo(BusinessInfo(
                            logo = imageUrl,
                            businessName = businessNames[index],
                            title = "紫薯黑米粥温暖你的胃",
                            promotion = arrayListOf("30减10", "蜂鸟不送", "新用户免单"),
                            distance = "5.1km",
                            deliveryTimeTake = "30分钟",
                            isPunctual = index == 0 || index == 5 || index == 9))
                            .isOfficialSupport(true)
                            .build()
            )

        }
    }

    fun dp2px(dpValue: Float): Float {
        val scale = resources.displayMetrics.density
        return dpValue * scale + 0.5f
    }

}
